<!-- 角色管理页面 -->
<%@ page language="java"  pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<script type="text/javascript" charset="utf-8">
$.formatString = function(str) { //自定义方法,实现datagrid中操作一栏的显示
	for ( var i = 0; i < arguments.length - 1; i++) {
		str = str.replace("{" + i + "}", arguments[i + 1]);
	}
	return str;
};
$.canEdit = true; //可点击
	$(function() {
		editRow=undefined;
		$('#admin_jsgl_datagrid').datagrid({
			url : ' ${pageContext.request.contextPath}/roleAction!datagrid.action ',
			title : '',
			iconCls : 'icon-save',
			pagination : true,
			pageSize : 5,
			pageList : [ 5, 10, 15, 20 ],
			fit : true,
			fitColumns : true,
			nowarp : false,
			border : false,
			idField : 'id',
			checkOnSelect:false,
			selectOnCheck:false,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 100,
				checkbox:true
			}, {
				field : 'rolename',
				title : '角色名称',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填'
						}
			           }
			},{
				field : 'description',
				title : '角色描述',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						missingMessage:'该项必填'
						}
			           }
			}, {
				field : 'action',
				title : '操作',
				width : 50,
				formatter : function(value, row, index) {
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img src="{0}" title="退货"/><a style="text-decoration:none" onclick="grantFun(\'{1}\');">授权</a>', '${pageContext.request.contextPath}/images/lock_add.png',row.id);
					}
					return str;
				}}] ],
			toolbar:'#toolbar',
			onAfterEdit:function(rowIndex,rowData,changes){
				var inserted=$('#admin_jsgl_datagrid').datagrid('getChanges','inserted');
				var updated=$('#admin_jsgl_datagrid').datagrid('getChanges','updated');
				if (inserted.length < 1 && updated.length < 1) {
					editRow = undefined;
					$('#admin_jsgl_datagrid').datagrid('unselectAll');
					$('#admin_jsgl_datagrid').datagrid('uncheckAll');
					return;
				}
				var url=' ';
				if(inserted.length>0){
					url='roleAction!add.action';
				}
				if(updated.length>0){
					url='roleAction!editor.action';
				}
				$.ajax({
					url:url,
					data:rowData,
					dataType:'json',
					success:function(r){
						if(r&&r.success){
							$('#admin_jsgl_datagrid').datagrid('acceptChanges');
							$('#admin_jsgl_datagrid').datagrid('load');
						    $.messager.show({
						    	title:'成功',
						    	msg:r.msg
						    	});
						}else{
							$('#admin_jsgl_datagrid').datagrid('rejectChanges');
						    $.messager.alert('错误',r.msg,'error');
						}
						editRow=undefined;
						$('#admin_jsgl_datagrid').datagrid('unselectAll');
						$('#admin_jsgl_datagrid').datagrid('uncheckAll');
					}
				});
			},
			onDblClickRow:function(rowIndex,rowData){
				if(editRow!=undefined){
					$('#admin_jsgl_datagrid').datagrid('endEdit',editRow);	
				}
				if(editRow==undefined){
				    $('#admin_jsgl_datagrid').datagrid('beginEdit',rowIndex);
				    editRow=rowIndex;
				    $('#admin_jsgl_datagrid').datagrid('unselectAll');
				    $('#admin_jsgl_datagrid').datagrid('uncheckAll');
				}
			} 
		});
		addFun=function(){  //添加
			  if(editRow!=undefined){
					$('#admin_jsgl_datagrid').datagrid('endEdit',editRow);
				}  
				if(editRow==undefined){
					$('#admin_jsgl_datagrid').datagrid('unselectAll');	
				    $('#admin_jsgl_datagrid').datagrid('insertRow',{
					       index:0,
					       row:{}
					 });
				$('#admin_jsgl_datagrid').datagrid('beginEdit',0);
				$('#admin_jsgl_datagrid').datagrid('unselectAll');
				$('#admin_jsgl_datagrid').datagrid('uncheckAll');
				editRow=0;
			    }
		};
		delFun=function(){ //删除
			var rows=$('#admin_jsgl_datagrid').datagrid('getChecked');
			if(rows.length>0){
				$.messager.confirm("请确认","您确定要删除当前所选择的记录吗?",function(b){
					if(b){
						var ids=[];
						for(var i=0;i<rows.length;i++){
							ids.push(rows[i].id);
						}
						$.ajax({
							url:'roleAction!del.action',
							data:{
								ids:ids.join(',')
								},
							dataType:'json',
							success:function(r){
								if(r&&r.success){
									$('#admin_jsgl_datagrid').datagrid('load');
									$('#admin_jsgl_datagrid').datagrid('unselectAll');
								    $.messager.show({
								    	title:'提示',
								    	msg:r.msg
								    	});
								}else{
									$('#admin_jsgl_datagrid').datagrid('rejectChanges');
									$('#admin_jsgl_datagrid').datagrid('unselectAll');
									$('#admin_jsgl_datagrid').datagrid('uncheckAll');
								    $.messager.alert('错误',r.msg,'error');
								}
							}
						});
					}
				});
			}else{
				  $.messager.alert('警告','未选中任何记录,请选择要删除的记录!','warning');			
			} 
		};
		editFun=function(){ //编辑
			var rows=$('#admin_jsgl_datagrid').datagrid('getChecked');
			console.info(rows.length);
			if(rows.length==1){
				if(editRow!=undefined){
					$('#admin_jsgl_datagrid').datagrid('endEdit',editRow);	
				}
				if(editRow==undefined){
					var index= $('#admin_jsgl_datagrid').datagrid('getRowIndex',rows[0]);
				    $('#admin_jsgl_datagrid').datagrid('beginEdit',index);
				    editRow=index;
				    $('#admin_jsgl_datagrid').datagrid('unselectAll');
				}
			}
			if(rows.length>1){
				$.messager.alert('警告','同一时间只能修改一条记录!','warning');
			}if(rows.length<1){
				$.messager.alert('警告','请选择要修改的记录!','warning');
			}
		};
		saveFun=function(){ //保存
			$('#admin_jsgl_datagrid').datagrid('endEdit',editRow);
		};
		undoEdit=function(){  //取消编辑
			editRow=undefined;
			$('#admin_jsgl_datagrid').datagrid('rejectChanges');
			$('#admin_jsgl_datagrid').datagrid('unselectAll');
			$('#admin_jsgl_datagrid').datagrid('uncheckAll');
		};
		unSelect=function(){ //取消选中
			$('#admin_jsgl_datagrid').datagrid('unselectAll');
			$('#admin_jsgl_datagrid').datagrid('uncheckAll');
		};
		refresh=function(){ //刷新
			$('#admin_jsgl_datagrid').datagrid('reload');
		};
		submitForm = function() { //权限设置提交方法
		var rows = $('#admin_jsgl_datagrid').datagrid('getChecked');
		var id = rows[0].id;
		console.info(id);
		var nodes = $('#grantTree').tree('getChecked', [ 'checked', 'indeterminate' ]);
		var ids = [];
		for (var i = 0; i < nodes.length; i++) {
			ids.push(nodes[i].id);
		}
		console.info(ids);
		if(rows.length==1){
		$.post('${pageContext.request.contextPath}/roleAction!grant.action', {
			id : id,
			ids : ids.join(',')
		}, function(result) {
			if (result.success) {
				grantDialog.dialog('destroy');
				$.messager.alert('提示', '授权成功！', 'info');
				$('#admin_jsgl_datagrid').datagrid('unselectAll');
				$('#admin_jsgl_datagrid').datagrid('uncheckAll');
			} else {
				$.messager.alert('提示', '授权失败!', 'error');
				$('#admin_jsgl_datagrid').datagrid('unselectAll');
				$('#admin_jsgl_datagrid').datagrid('uncheckAll');
			}
		}, 'json');
		}
		if(rows.length>1){
			$.messager.alert('警告','同一时间只能对一个角色授权!','warning');
		}if(rows.length<1){
			$.messager.alert('警告','请选择要授权的角色!','warning');
		}
	};
		grantFun=function(id){ //角色授权
			grantDialog=parent.dialog({
				title : '角色授权',
				href : '${pageContext.request.contextPath}/admin/roleGrant.jsp?id=' + id,
				width : 350,
				height : 500,
				buttons : [ {
					text : '授权',
					iconCls :'icon-ok',
					handler : function() {
						submitForm();
					}
				} ,{
					text : '取消',
					iconCls :'icon-no',
					handler : function() {
					    grantDialog.dialog('close');
					    $('#admin_jsgl_datagrid').datagrid('unselectAll');
					    $('#admin_jsgl_datagrid').datagrid('uncheckAll');
					}
				}]		    
			});	
		};
	});
</script>
<div id="admin_jsgl_layout" class="easyui-layout" data-options="border:false,fit:true">
<div region="center" data-options="border:false">
<table id="admin_jsgl_datagrid"></table>
</div>
<div id="toolbar" style="display: none;">
		<table>
			<tr>
				<shiro:hasPermission name="sys:jsgl:add">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addFun();">添加</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:jsgl:del">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="delFun();">删除</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:jsgl:edit">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editFun();">修改</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:jsgl:edit"><td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="saveFun();">保存</a></td></shiro:hasPermission>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="undoEdit();">取消编辑</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="unSelect();">取消选中</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="refresh();">刷新</a></td>
			</tr>
		</table>
	</div>
</div>


